import java.lang.String;

public class LeetCode_14 {
    public static void main(String[] args) {
        LeetCode_14 leet = new LeetCode_14();
        String strs[] = {"flower","flow","flight"};
        System.out.println(leet.longestCommonPrefix(strs));
    }

    public String  longestCommonPrefix(String[]strs){
        if(strs == null || strs.length==0){
            return "";
        }

        //prefix 前缀
        String prefix = strs[0];
        for(int i =1;i<strs.length;i++){
            int j=0;
            while((j<prefix.length() &&  j<strs[i].length())&& prefix.charAt(j) == strs[i].charAt(j)){
                j++;
            }
            prefix = prefix.substring(0,j);
            if(prefix.isEmpty()){
                return "";
            }
        }

        return prefix;

    }
   /* public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }

        String prefix = strs[0];
        for (int i = 1; i < strs.length; i++) {
            int j = 0;
            // 只比较到当前已知的最短字符串的长度
            while ((j < prefix.length() && j < strs[i].length()) && (prefix.charAt(j) == strs[i].charAt(j))) {
                j++;
            }
            // 更新prefix为当前找到的最长公共前缀
            prefix = prefix.substring(0, j);
            // 如果prefix为空，则没有公共前缀
            if (prefix.isEmpty()) {
                return "";
            }
        }
        return prefix;
    }*/
    /*public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }

        String prefix = strs[0];
        for (int i = 1; i < strs.length; i++) {

            while (strs[i].indexOf(prefix) != 0) {
                prefix = prefix.substring(0, prefix.length() - 1);
                if (prefix.isEmpty()) {
                    return "";
                }
            }
        }
        return prefix;
    }*/
 /*   public String longestCommonPrefix(String[] strs) {
        int flag = 0;
        int flag1 = 0;
        int figure = 0;
        int numb = 1;



            char ch1[] = strs[0].toCharArray();
            char ch2[] = strs[1].toCharArray();
            char ch3[] = strs[2].toCharArray();
            char ch4[] = new char[2];
           for (int i = 0; i < ch1.length; i++) {
              for (int j = 0; j < ch2.length; j++) {
                  if (ch1[i] == ch2[j]) {
                      flag = 1;
                      break;
                  }
              }
              if (flag == 1) {
                  flag = 0;
                  for (int j = 0; j < ch3.length; j++) {
                      if (ch1[i] == ch3[j]) {
                          flag1 = 1;
                          if (i+1 != ch1.length){
                              numb++;
                          }
                          ch4[figure] = ch1[i];
                          figure++;

                         break;
                      }
                  }
              }
           }

           if (flag1==1){
               String sb = new String(ch4);
               return sb;

           }else {
              return "";
           }
*/

    }

 /*   输入：strs = ["flower","flow","flight"]
    输出："fl"

    输入：strs = ["dog","racecar","car"]
输出：""
解释：输入不存在公共前缀。

            1 <= strs.length <= 200
            0 <= strs[i].length <= 200
             strs[i] 仅由小写英文字母组成*/
